import numpy as np
import matplotlib.pyplot as plt
import cv2

img = cv2.imread('./img/1101_1/re (2).tif',1)
print('img info:')
print('shape:',img.shape,' pixal num:',img.size,' dtype:',img.dtype)
zeros = np.zeros(img.shape[:2], dtype = "uint8")
mask= np.zeros(img.shape[:2], dtype = "uint8")
#res = np.zeros(img.shape[:2], dtype = "uint8")
#不推荐
#img[10][10]=[0,0,0]
#print(img[10][10])  
#矩阵方法，推荐
# 获取像素值
#print (img.item(10,10,0),img.item(10,10,1),img.item(10,10,2))
# 修改像素值
#img.itemset((1000,1000,2),255)
#print (img.item(10,10,2))
#提取图片部分ROI
#img1=img[100:100,200:200]
#img[200:200,300:300]=img1
#RGB拆分
b,g,r=cv2.split(img)
#RGB组合br
#img2=cv2.merge([b,g,r])
def get_gray_value(img,pos,harf_d):
    sum = 0
    count = 0
    start_point=[0,0]
    end_point = [0,0]
    start_point[0]=pos[0]-harf_d
    start_point[1]=pos[1]-harf_d

    end_point[0]=pos[0]+harf_d
    end_point[1]=pos[1]+harf_d
    #print(start_point[0],end_point[0])
    for i in range(start_point[0],end_point[0]+1):#行遍历
        #print(i)
        for j in range(start_point[1],end_point[1]+1):#行遍历
            #print(i,j)
            sum += img[i,j]
            count += 1
    avg=sum/count
    return img[pos[0]][pos[1]],round(avg,2)





#鼠标回调函数 
#事件 坐标 标志 返回

def show_num(event,y,x,flags,param):
    global mask
    #半径
    rad=40
    if event==cv2.EVENT_LBUTTONUP:

        cv2.circle(mask,(y,x),rad,(255,0,0),-1)
        res=cv2.bitwise_and(pro_img_tmp,pro_img_tmp,mask=mask)
        #cv2.imshow('res',res)
        plt.hist(res.ravel(),256,[1,256]);#灰度直方图
        #plt.show()
        #print('shape:',res.shape,' pixal num:',res.size,' dtype:',res.dtype)
        
        #s=np.pi*((rad)**2)
        ave=np.sum(res)/np.sum(res>0)
        print(np.sum(res),"    ",ave)
        
        #print(np.sum(res),"    ",np.sum(res)/s)
        #print(np.sum(res>0),'   ',s)
        res=np.zeros(img.shape[:2], dtype = "uint8")
        mask=np.zeros(img.shape[:2], dtype = "uint8")

        #print('POS:(',x,y,') R:',r,'G:',g,'B:',b)
        #print('POS:(',x,y,')' ,img.item(x,y,0),img.item(x,y,1),img.item(x,y,2))
        print('POS:(',x,y,')' ,r[x][y])
        #ave=get_gray_value(r,[x,y],d)
        
        print('average :(',x,y,')' ,ave)
        cv2.circle(pro_img,(y,x),rad,(0,255,0),1)
        cv2.circle(pro_img,(y,x),1,(0,255,0),-1)
        cv2.putText(pro_img,str(round(ave,2)),(y+15,x-15), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,65,255),1)
        
        
    elif event==cv2.EVENT_MBUTTONDOWN:
        #print(num)
        plt.show()
cv2.namedWindow('image')
#cv2.imshow('image',img)
#cv2.imshow('image',r)
#cv2.imshow('image3',(r+g)/(r-g))
#cv2.imshow('image_B',cv2.merge([b, zeros, zeros]))#带颜色的单通道灰度图
#cv2.imshow('image_G',cv2.merge([zeros, g, zeros]))#带颜色的单通道灰度图
#cv2.imshow('image_R',cv2.merge([zeros, zeros, r]))#带颜色的单通道灰度图
cv2.setMouseCallback('image',show_num)
pro_img=cv2.merge([r, r, r])
pro_img_tmp=r
#cv2.imshow('red',r)
#cv2.circle(pro_img,(100,100),1,(0,0,255),-1)
d=20

#cv2.circle(mask,(100,200),d*2,(255,0,0),-1)
#cv2.imshow('mask',mask)


while(1):
    cv2.imshow('image',pro_img)
        
    key=cv2.waitKey(1)&0xFF
    if key==27:
        break
    if cv2.getWindowProperty('image', cv2.WND_PROP_AUTOSIZE) < 1:
        break
cv2.destroyAllWindows()       